﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace YouFood.Views.Login
{
    public partial class YouFoodLoginForm : ChildWindow, INotifyPropertyChanged
    {

        #region Property

        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set { _userName = value;
            NotifyPropertyChanged("UserName");
            }
        }


        private string _userPassword;
        public string UserPassword
        {
            get { return _userPassword; }
            set
            {
                _userPassword = value;
                NotifyPropertyChanged("UserPassword");
            }
        }

        public bool IsInPasswordMode { get; set; }

        #endregion

        public YouFoodLoginForm()
        {
            InitializeComponent();

            DataContext = this;

            this.MyImage.Source = new BitmapImage(new Uri("/YouFood;component/Assets/Images/Enter.png", UriKind.Relative));
            this.UserLabel.Text = "User :";
            UserPasswordBox.Visibility = Visibility.Collapsed;
            
        }

        private void Enter(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                LogonUser();
            else
                PasswordMode();
        }

        private void LogonUser()
        {
            var login = new LoginParameters(UserName, UserPassword, false, null);
            WebContext.Current.Authentication.Login(login, this.LoginOperation_Completed, null);
        }

        private void LoginOperation_Completed(LoginOperation loginOperation)
        {

            // TODO getion error and success
            if (loginOperation.LoginSuccess)
            {
                Logged(this, null);
                this.Close();
            }
            else if (loginOperation.HasError)
            {
                ErrorWindow.CreateNew(loginOperation.Error);
                loginOperation.MarkErrorAsHandled();
            }
            else if (!loginOperation.IsCanceled)
            {
                //this.loginInfo.ValidationErrors.Add(new ValidationResult(ErrorResources.ErrorBadUserNameOrPassword, new string[] { "UserName", "Password" }));
            }
        }

        public event EventHandler Logged;

        private void Reset(object sender, RoutedEventArgs e)
        {
            
            UserMode();
        }

        private void UserMode()
        {
            IsInPasswordMode = false;
            this.MyImage.Source = new BitmapImage(new Uri("/YouFood;component/Assets/Images/Enter.png", UriKind.Relative));
            this.UserLabel.Text = "User :";
            UserPasswordBox.Visibility = Visibility.Collapsed;
            UserNameBox.Visibility = Visibility.Visible;
            UserName = string.Empty;
            UserPassword = string.Empty;
            UserNameBox.Focus();
        }

        private void PasswordMode()
        {
            IsInPasswordMode = true;
            this.MyImage.Source = new BitmapImage(new Uri("/YouFood;component/Assets/Images/Login.png", UriKind.Relative));
            this.UserLabel.Text = "Password :";
            UserPasswordBox.Visibility = Visibility.Visible;
            UserNameBox.Visibility = Visibility.Collapsed;
            UserPasswordBox.Focus();

        }

        private void Number1(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                UserPassword += "1";
            else
                UserName += "1";
        }

        private void Number2(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                UserPassword += "2";
            else
                UserName += "2";
        }

        private void Number3(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                UserPassword += "3";
            else
                UserName += "3";
        }

        private void Number4(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                UserPassword += "4";
            else
                UserName += "4";
        }

        private void Number5(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                UserPassword += "5";
            else
                UserName += "5";
        }

        private void Number6(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                UserPassword += "6";
            else
                UserName += "6";
        }

        private void Number7(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                UserPassword += "7";
            else
                UserName += "7";
        }

        private void Number8(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                UserPassword += "8";
            else
                UserName += "8";
        }

        private void Number9(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                UserPassword += "9";
            else
                UserName += "9";
        }

        private void Number0(object sender, RoutedEventArgs e)
        {
            if (IsInPasswordMode)
                UserPassword += "0";
            else
                UserName += "0";
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

